package com.xyh.transaction.controller;

import com.github.pagehelper.PageInfo;
import com.xyh.transaction.entity.dto.category.CategoryDto;
import com.xyh.transaction.entity.dto.category.CategoryGoodsDto;
import com.xyh.transaction.entity.dto.user.ArrDto;
import com.xyh.transaction.entity.vo.CategorySubVo;
import com.xyh.transaction.entity.vo.CategoryVo;
import com.xyh.transaction.entity.vo.GoodsVo;
import com.xyh.transaction.response.ResponseResult;
import com.xyh.transaction.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @ApiOperation(value = "分类", notes = "根据id返回产品大类以及子类")
    @GetMapping()
//    @PreAuthorize("hasAuthority('Read')")
//    @PreAuthorize("@ex.hasAuthority('Execute')")// 使用自定义的权限校验方法
    public ResponseResult<CategoryVo> getCategoryVo(@RequestParam(required = false) String id) {
        CategoryVo categoryVo = categoryService.getCategoryVo(id);

        return new ResponseResult<>(200, "操作成功", categoryVo);
    }

    @ApiOperation(value = "分类列表", notes = "返回所有产品大类及子类")
    @PostMapping("list")
    public ResponseResult<PageInfo<CategoryVo>> list(@RequestBody CategoryDto categoryDto) {
        PageInfo<CategoryVo> categoryList = categoryService.getCategoryList(categoryDto);

        return ResponseResult.success("查询成功！", categoryList);
    }

    @ApiOperation(value = "修改分类信息", notes = "")
    @PutMapping("edit")
    public ResponseResult<Void> editCategory(@RequestBody CategoryDto categoryDto) {
        categoryService.updateCategory(categoryDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "添加分类", notes = "添加二级分类信息，一级分类写死，只能通过数据库新增")
    @PostMapping("add")
    public ResponseResult<Void> addCategory(@RequestBody CategoryDto categoryDto) {
        categoryService.addCategory(categoryDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "删除分类", notes = "根据id[]删除产品分类")
    @PostMapping("delete")
    public ResponseResult<Void> deleteCategory(@RequestBody ArrDto arrDto) {
        categoryService.deleteCategory(arrDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "二级分类", notes = "根据id返回二级分类以及子类")
    @GetMapping("/sub/filter")
    public ResponseResult<CategorySubVo> subFilter(@RequestParam Integer id) {
        CategorySubVo categorySubVos = categoryService.getCategorySub(id);

        return ResponseResult.success("查询成功！", categorySubVos);
    }

    @ApiOperation(value = "二级分类列表", notes = "返回所有二级分类列表")
    @GetMapping("/sub/list")
    public ResponseResult<List<CategorySubVo>> subList() {
        List<CategorySubVo> subList = categoryService.getSubList();
        return ResponseResult.success("查询成功！", subList);
    }

    @ApiOperation(value = "商品列表", notes = "根据categoryId返回商品列表")
    @PostMapping("goods")
    public ResponseResult<PageInfo<GoodsVo>> goodsList(@RequestBody CategoryGoodsDto cg) {
        PageInfo<GoodsVo> goodsVoPageInfo = categoryService.getGoodsList(cg);

        return ResponseResult.success("查询成功！", goodsVoPageInfo);
    }


}
